package towa.com.remoteWMP;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PlaylistActivity extends Activity {

	private ArrayList<SongWrapper> playlist;
	private String playlistName;
	private ListView list;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlist);
		
//		playlist = new ArrayList<SongWrapper>();
//		playlist.add(new SongWrapper(1, "One"));
//		playlist.add(new SongWrapper(2, "Two"));
//		playlist.add(new SongWrapper(3, "Three"));
		Bundle b = getIntent().getBundleExtra("playlist");
		if(b != null){
			playlistName = b.getString("playlistName");
			playlist = (ArrayList<SongWrapper>) b.getSerializable("playlist");
		}
		if(playlist == null){
			playlist = new ArrayList<SongWrapper>();
			playlist.add(new SongWrapper(-1, "No Current Playlist"));
		}
		
		if(playlistName != null)
			setTitle("Playlist: " + playlistName);
		
		if (savedInstanceState != null)
			savedInstanceState.putSerializable("playlist", playlist);
		
		list = (ListView) findViewById(R.id.list);
		ArrayAdapter<SongWrapper> adapter = new ArrayAdapter<SongWrapper>(this, android.R.layout.simple_list_item_1, playlist);
		list.setAdapter(adapter);
		list.setClickable(true);
		list.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				SongWrapper sw = (SongWrapper) arg0.getItemAtPosition(arg2);
				Intent intent = new Intent(PlaylistActivity.this, RemoteWMPActivity.class);
				intent.putExtra("SongSelected", sw.getId());
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
			
		});
	}
	
	@Override
	public void onPause() {
		super.onPause();
//		playlist.add(new SongWrapper(4, "MY SAWNG"));
//		bundle.putSerializable("playlist", playlist);
	}
	
//	@Override
//	public void onResume() {
//		super.onResume();
////		playlist.add(new SongWrapper(4, "MY SAWNG234"));
//		Log.i("WMPRemote", playlist.toString());
//		playlist = (ArrayList<SongWrapper>) bundle.getSerializable("playlist");
////		ArrayList<SongWrapper> p2 = (ArrayList<SongWrapper>) bundle.getSerializable("playlist");
//		Log.i("WMPRemote", playlist.toString() + " 2");
//	}
	
	@Override
	public void onSaveInstanceState(Bundle bundle) {
		bundle.putSerializable("playlist", playlist);
		super.onSaveInstanceState(bundle);
	}
	
	@Override
	public void onRestoreInstanceState(Bundle bundle) {
		super.onRestoreInstanceState(bundle);
		playlist = (ArrayList<SongWrapper>) bundle.getSerializable("playlist");
	}
	
}
